<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\admin\service\RoleService;
use app\Request;
use think\facade\View;

class Role extends AdminController
{
    /**
     * 角色列表
     *
     * @Author tanruizheng
     * @DateTime 2021-07-09
     * @return void
     */
    public function index(Request $request){
        if($request->param('_action') == 'getRoleList'){
            $where = [];
            $status = $request->param('status','');
            if($status !== ''){
                $where[] = ['status','=',$status];
            }
            $roleList = RoleService::getRoleList($where);
            return json($roleList);
        }

        return view::fetch('index');
    }

    /**
     * 角色添加
     *
     * @Author tanruizheng
     * @DateTime 2021-07-13
     * @param Request $request
     * @return void
     */
    public function add(Request $request){
        if($request->isPost()){
            $params = $request->param();
            $result = RoleService::addOrEditData($params);
            return json($result);
        }
        $roleList = RoleService::getRoleList()['data'];
        return View::fetch('add',[
            'roleList' => $roleList
        ]);
    }

    /**
     * 角色编辑
     *
     * @Author tanruizheng
     * @DateTime 2021-07-13
     * @param Request $request
     * @return void
     */
    public function edit(Request $request){
        if($request->isPost()){
            $params = $request->param();
            $result = RoleService::addOrEditData($params);
            return json($result);
        }
        $roleList = RoleService::getRoleList()['data'];
        $roleId = $request->param('id',0);
        $roleDetail = RoleService::getRoleDetail($roleId);
        return View::fetch('edit',[
            'roleList' => $roleList,
            'roleDetail' => $roleDetail
        ]);
    }

    /**
     * 角色删除
     *
     * @Author tanruizheng
     * @DateTime 2021-07-13
     * @param Request $request
     * @return void
     */
    public function delete(Request $request){
        $roleId = $request->param('id',0);
        if(!$roleId){
            return self::createFail('参数错误');
        }
        $result = RoleService::roleDelete($roleId);
        return json($result);
    }

    /**
     * 权限设置
     *
     * @Author tanruizheng
     * @DateTime 2021-07-14
     * @param Request $request
     * @return void
     */
    public function authorize(Request $request){
        $roleId = $request->param('id',0);//角色ID
        
        //获取权限列表
        if($request->param('_action') == 'getAuthorizeList'){
            $authorizeList = RoleService::getAuthorizeList($roleId);
            return json($authorizeList);
        }

        //权限更新
        if($request->param('_action') == 'authorize'){
            $authids = $request->param('authids');
            //更新权限
            $result = RoleService::authorize($roleId,$authids);
            return json($result);
        }
        return View::fetch('authorize',[
           'roleId' => $roleId
        ]);
    }
}
